//
//  main.m
//  OC05_遍历与排序
//
//  Created by Hanlingyu on 16/7/8.
//  Copyright © 2016年 ltq. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "Person.h"
int main(int argc, const char * argv[]) {
    
    // 1. 创建需要遍历的数组
    NSArray *array = @[@"陆畅", @"张建", @"刘思雨", @"张丁懿", @"刘坦奇"];
    // 2. 创建可变数组, 接受倒叙的值
    NSMutableArray *newArray = [NSMutableArray array];
    // 3. 倒叙遍历
//    for (循环变量; 循环条件; 循环增量) {
//        循环体
//    }
    for (NSInteger i = array.count - 1; i >= 0; i--) {
        [newArray addObject:array[i]];
    }
    NSLog(@"%@", newArray);
    
    // 定义一个字典, 用于存储一个人的name, sex, age三个数据, 完成对其三个数据的输出.
    NSDictionary *personDic = @{@"name": @"刘坦奇",
                                 @"sex": @"男",
                                 @"age": @23};
    NSArray *allKeys = personDic.allKeys;
    for (NSInteger i = 0; i < allKeys.count; i++) {
        NSString *key = allKeys[i];
        id value = [personDic objectForKey:key];
        NSLog(@"key = %@, value = %@", key, value);
    }
   
    // 枚举器
    // NSWnumerator 依赖于集合类型(数组, 字典, 集合), 不能单独使用
    // 正序枚举
    NSEnumerator *stringArrayEnumerator = [array objectEnumerator];
    id value = nil;
    while ((value = [stringArrayEnumerator nextObject]) != nil) {
        NSLog(@"value = %@", value);
    }
    // 倒叙枚举
    NSEnumerator *reverse = [array reverseObjectEnumerator];
    id reverseValue = nil;
    while (reverseValue = [reverse nextObject]) {
        NSLog(@"reverseValue = %@", reverseValue);
    }
    
    // 字典枚举器                             ↓ 若想打印key, 就在箭头下加 .allKeys
    NSEnumerator *dicenumerator = [personDic objectEnumerator];
    id dicValue = nil;
    while (dicValue = [dicenumerator nextObject]) {
        NSArray *keys = [personDic allKeysForObject:dicValue];
        NSLog(@"keys = %@, value = %@", keys, dicValue);
    }
    
    // 集合枚举 (无序, 没有倒叙枚举)
    NSSet *set = [NSSet setWithObjects:@"110", @"120", @"119", @"114", @"12315", nil];
    NSEnumerator *setEnumerator = [set objectEnumerator];
    id setValue = nil;
    while (setValue = [setEnumerator nextObject]) {
        NSLog(@"setValue = %@", setValue);
    }
    
    
    // 快速枚举 for...in (是对NSEnumerator的封装)
    // forin正序
    for (NSString *string in array) {
        NSLog(@"%@", string);
    }
    // forin倒叙
    for (NSString *string in [array reverseObjectEnumerator]) {
        NSLog(@"%@", string);
    }
    
    // 定义一个数组, 包含5个字符串对象, 取出数组中所有字符串, 并拼接成一个新的字符串.
    
    NSArray *nameArr = @[@"陆畅", @"张建", @"刘思雨", @"张丁懿", @"刘坦奇"];
    NSMutableString *totalStr = [NSMutableString string];
    for (NSString *name in nameArr) {
        [totalStr appendString:name];
    }
    NSLog(@"%@", totalStr);
    
    // 枚举一个字典
    for (NSString *key in personDic) {
        NSLog(@"key = %@", key);
        NSLog(@"value = %@", [personDic objectForKey:key]);
    }
    
    // 快速枚举一个集合
    for (NSString *anyObject in set) {
        NSLog(@"anyObject = %@", anyObject);
    }
    
    
    // 排序
    // 参数1: key :排序条件
    // 参数2 acending: YES(升序) NO(降序)
    NSArray *arr = @[@1, @2, @3, @4, @5];
    NSMutableArray *mutableArr = [NSMutableArray arrayWithObjects:@1, @12, @23, @44, @55, nil];
    NSSortDescriptor *sortDescriptor = [[NSSortDescriptor alloc] initWithKey:@"self" ascending:YES];
    // 数组有排序的方法(sortedArrayUsingDescriptors)
    // 参数为一个排序条件的数组
    arr = [arr sortedArrayUsingDescriptors:@[sortDescriptor]];
    //[mutableArr sortedArrayUsingDescriptors:@[sortDescriptor]];
    [mutableArr sortUsingDescriptors:@[sortDescriptor]];
    NSLog(@"%@", arr);
    NSLog(@"%@", mutableArr);
    // 排序: 可变数组和不可变数组通过sort排序调用的方法不同
    // 1. 不可变数组排序后, 需要生成新的数组
    // 2. 可变数组排序可以直接在本身上修改
    
    
    
    
    Person *per1 = [[Person alloc] init];
    Person *per2 = [[Person alloc] init];
    Person *per3 = [[Person alloc] init];

    per1.name = @"baby";
    per2.name = @"baby";
    per3.name = @"败老闹";
    
    per1.age = 100;
    per2.age = 50;
    per3.age = 200;
    Person *per4 = [[Person alloc] initWithName:@"呦呦" age:20];
    Person *per5 = [[Person alloc] initWithName:@"切克闹" age:44];
    
    NSArray *perArray = @[per1, per2, per3, per4, per5];
    NSLog(@"排序前: %@", perArray);
    // 根据age排序
    NSSortDescriptor *perSort = [[NSSortDescriptor alloc] initWithKey:@"age" ascending:YES];
    perArray = [perArray sortedArrayUsingDescriptors:@[perSort]];
    NSLog(@"排序后: %@", perArray);
    
    
    // 排序2 sotredArrayUsingSelector
    NSArray *sortString = @[@"iphone", @"ipad", @"iTouch", @"iMac", @"iMac Mini"];
    sortString = [sortString sortedArrayUsingSelector:@selector(compare:)];
    NSLog(@"%@", sortString);
    
    
    return 0;
}
